/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 有点呆
 * Date: 2024-10-26
 * Time: 17:37
 */

import java.util.Scanner;

public class FCFS {
    public static void main(String[] args) {
        JCB[] a = new JCB[10];
        Scanner scanner = new Scanner(System.in);

        int k = 0;//初始化
        System.out.println("请一次输入两个整数，之间用空格隔开。输入负数结束输入");

        //重复输入进程信息
        while (true) {
            System.out.printf("请输入第%d进程到达时间、服务时间：", k);
            //设置进程id为k
            double arriveTime = scanner.nextDouble();
            if (arriveTime < 0) {
                System.out.println("输入结束");
                break;
            }
            double serviceTime = scanner.nextDouble();
            double finishTime = 0;
            double float_wi = 0;
            double cyclingTime = 0;
            a[k] = new JCB(k, arriveTime, serviceTime, finishTime, cyclingTime, float_wi);//利用构造方法初始化数据
            k++;
            if (serviceTime < 0) {
                System.out.println("输入结束");
                break;
            }

        }

        scanner.close();//释放scanner

        //输出初始作业状态
        System.out.println("初始作业状态：");
        for (int i = 0; i < k; i++) {
            System.out.println(a[i]);
        }

        //冒泡排序，按照到达时间从小到大排序
        for (int i = 0; i < k; i++) {
            for (int j = 0; j + 1 < k - 1; j++) {
                if (a[j].getArriveTime() > a[j + 1].getArriveTime()) {
                    JCB temp = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = temp;
                }
            }
        }

        //遍历数组。利用算法完善数据并输出结果
        System.out.println();
        System.out.println("调度过程及调度后作业状态：");
        for (int i = 0; i < k; i++) {
            if (i == 0) {
                //完成时间=到达时间+服务时间（运行）
                a[i].setFinishTime(a[i].getArriveTime() + a[i].getServiceTime());
                //周转时间=完成时间-到达时间(提交)
                a[i].setCyclingTime(a[i].getFinishTime() - a[i].getArriveTime());

                //将结果保留两位小数
                double time = a[i].getCyclingTime() / a[i].getServiceTime();
                a[i].setFloat_wi(Double.parseDouble(String.format("%.2f", time)));
            } else if (a[i].getArriveTime() > a[i - 1].getFinishTime()) {
                //到达时间>完成时间
                a[i].setFinishTime(a[i].getArriveTime() + a[i].getServiceTime());

            } else {
                a[i].setFinishTime(a[i - 1].getFinishTime() + a[i].getServiceTime());

            }
            //周转时间=完成时间-到达时间(提交)
            a[i].setCyclingTime(a[i].getFinishTime() - a[i].getArriveTime());

            //结果保留两位小数
            double time = a[i].getCyclingTime() / a[i].getServiceTime();
            a[i].setFloat_wi(Double.parseDouble(String.format("%.2f", time)));
        }

        int i = 0;
        //遍历数组输出结果
        System.out.println(a[i]);

    }

    public static class JCB {

        private int id;
        private double arriveTime;//到达时间
        private double serviceTime;//服务时间
        private double finishTime;//完成时间
        private double cyclingTime;//周转时间
        private double float_wi;//带权周转时间

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public double getArriveTime() {
            return arriveTime;
        }

        public void setArriveTime(double arriveTime) {
            this.arriveTime = arriveTime;
        }

        public double getServiceTime() {
            return serviceTime;
        }

        public void setServiceTime(double serviceTime) {
            this.serviceTime = serviceTime;
        }

        public double getFinishTime() {
            return finishTime;
        }

        public void setFinishTime(double finishTime) {
            this.finishTime = finishTime;
        }

        public double getCyclingTime() {
            return cyclingTime;
        }

        public void setCyclingTime(double cyclingTime) {
            this.cyclingTime = cyclingTime;
        }

        public double getFloat_wi() {
            return float_wi;
        }

        public void setFloat_wi(double float_wi) {
            this.float_wi = float_wi;
        }

        public JCB() {

        }

        public JCB(int id, double arriveTime, double serviceTime, double finishTime, double cyclingTime,
                   double float_wi) {
            super();
            this.id = id;
            this.arriveTime = arriveTime;
            this.serviceTime = serviceTime;
            this.finishTime = finishTime;
            this.cyclingTime = cyclingTime;
            this.float_wi = float_wi;
        }

        @Override
        public String toString() {
            return "进程信息" + "id=" + id +
                    ", arriveTime=" + arriveTime +
                    ", serviceTime=" + serviceTime +
                    ", finishTime=" + finishTime +
                    ", cyclingTime=" + cyclingTime +
                    ", float_wi=" + float_wi;
        }
    }
}
